/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.softvfc.charthandler.base;

import java.util.Iterator;
import java.util.Set;
import java.util.logging.Logger;
import org.openide.util.NbBundle;
import org.openide.windows.TopComponent;
import org.openide.windows.WindowManager;
import org.openide.util.ImageUtilities;
import org.netbeans.api.settings.ConvertAsProperties;
import org.softvfc.charthandler.xyseries.XYSeriesRenderer;
 
/**
 * Top component which displays something.
 */
@ConvertAsProperties(dtd = "-//org.softvfc.charthandler.base//ChartWindow//EN",
autostore = false)
public final class ChartWindowTopComponent extends TopComponent {

    private static ChartWindowTopComponent instance;
    /** path to the icon used by the component and its open action */
    static final String ICON_PATH = "org/softvfc/charthandler/base/stock_chart-edit-type.png";
    private static final String PREFERRED_ID = "ChartWindowTopComponent";

     
    public ChartWindowTopComponent() {
        initComponents();
        setName(NbBundle.getMessage(ChartWindowTopComponent.class, "CTL_ChartWindowTopComponent"));
        setToolTipText(NbBundle.getMessage(ChartWindowTopComponent.class, "HINT_ChartWindowTopComponent"));
        setIcon(ImageUtilities.loadImage(ICON_PATH, true));


    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jToolBar1 = new javax.swing.JToolBar();
        jbRefreshDataSource = new javax.swing.JButton();
        jcbDataSource = new javax.swing.JComboBox();
        jSeparator1 = new javax.swing.JToolBar.Separator();
        jbPreviewBC = new javax.swing.JButton();
        jSplitPane1 = new javax.swing.JSplitPane();
        jPanel2 = new javax.swing.JPanel();
        jtpPreview = new javax.swing.JTabbedPane();

        jToolBar1.setFloatable(false);
        jToolBar1.setRollover(true);

        jbRefreshDataSource.setIcon(new javax.swing.ImageIcon(getClass().getResource("/org/softvfc/charthandler/base/view-refresh.png"))); // NOI18N
        org.openide.awt.Mnemonics.setLocalizedText(jbRefreshDataSource, org.openide.util.NbBundle.getMessage(ChartWindowTopComponent.class, "ChartWindowTopComponent.jbRefreshDataSource.text")); // NOI18N
        jbRefreshDataSource.setToolTipText(org.openide.util.NbBundle.getMessage(ChartWindowTopComponent.class, "ChartWindowTopComponent.jbRefreshDataSource.toolTipText")); // NOI18N
        jbRefreshDataSource.setFocusable(false);
        jbRefreshDataSource.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        jbRefreshDataSource.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        jbRefreshDataSource.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbRefreshDataSourceActionPerformed(evt);
            }
        });
        jToolBar1.add(jbRefreshDataSource);

        jcbDataSource.setActionCommand(org.openide.util.NbBundle.getMessage(ChartWindowTopComponent.class, "ChartWindowTopComponent.jcbDataSource.actionCommand")); // NOI18N
        jcbDataSource.setPreferredSize(new java.awt.Dimension(150, 27));
        jToolBar1.add(jcbDataSource);
        jToolBar1.add(jSeparator1);

        org.openide.awt.Mnemonics.setLocalizedText(jbPreviewBC, org.openide.util.NbBundle.getMessage(ChartWindowTopComponent.class, "ChartWindowTopComponent.jbPreviewBC.text")); // NOI18N
        jbPreviewBC.setFocusable(false);
        jbPreviewBC.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        jbPreviewBC.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        jbPreviewBC.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbPreviewBCActionPerformed(evt);
            }
        });
        jToolBar1.add(jbPreviewBC);

        jSplitPane1.setOrientation(javax.swing.JSplitPane.VERTICAL_SPLIT);
        jSplitPane1.setMinimumSize(new java.awt.Dimension(0, 0));

        jPanel2.setBackground(new java.awt.Color(254, 254, 254));

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 762, Short.MAX_VALUE)
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 208, Short.MAX_VALUE)
        );

        jSplitPane1.setTopComponent(jPanel2);

        jtpPreview.setMinimumSize(new java.awt.Dimension(0, 0));
        jtpPreview.setPreferredSize(new java.awt.Dimension(720, 148));
        jSplitPane1.setBottomComponent(jtpPreview);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jToolBar1, javax.swing.GroupLayout.DEFAULT_SIZE, 762, Short.MAX_VALUE)
            .addComponent(jSplitPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 762, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jToolBar1, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jSplitPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 384, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void jbRefreshDataSourceActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbRefreshDataSourceActionPerformed
        jcbDataSource.removeAllItems();
        TopComponent.Registry reg = TopComponent.getRegistry();
        Set<TopComponent> opened = reg.getOpened();
        for (Iterator<TopComponent> it = opened.iterator(); it.hasNext(); ) {
            String displayName = it.next().getDisplayName(); 
            if (displayName!=null && displayName.startsWith("Table")){
                jcbDataSource.addItem(displayName);

            }

        }
    }//GEN-LAST:event_jbRefreshDataSourceActionPerformed

    private void jbPreviewBCActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbPreviewBCActionPerformed
        XYSeriesRenderer bcSeriesRenderer = new XYSeriesRenderer("BC");
        jtpPreview.addTab(bcSeriesRenderer.getTitle(), bcSeriesRenderer.getChartPanel());
        initTabComponent(jtpPreview.getTabRunCount()-1);
    }//GEN-LAST:event_jbPreviewBCActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JPanel jPanel2;
    private javax.swing.JToolBar.Separator jSeparator1;
    private javax.swing.JSplitPane jSplitPane1;
    private javax.swing.JToolBar jToolBar1;
    private javax.swing.JButton jbPreviewBC;
    private javax.swing.JButton jbRefreshDataSource;
    private javax.swing.JComboBox jcbDataSource;
    private javax.swing.JTabbedPane jtpPreview;
    // End of variables declaration//GEN-END:variables
    /**
     * Gets default instance. Do not use directly: reserved for *.settings files only,
     * i.e. deserialization routines; otherwise you could get a non-deserialized instance.
     * To obtain the singleton instance, use {@link #findInstance}.
     */
    public static synchronized ChartWindowTopComponent getDefault() {
        if (instance == null) {
            instance = new ChartWindowTopComponent();
        }
        return instance;
    }

    /**
     * Obtain the ChartWindowTopComponent instance. Never call {@link #getDefault} directly!
     */
    public static synchronized ChartWindowTopComponent findInstance() {
        TopComponent win = WindowManager.getDefault().findTopComponent(PREFERRED_ID);
        if (win == null) {
            Logger.getLogger(ChartWindowTopComponent.class.getName()).warning(
                    "Cannot find " + PREFERRED_ID + " component. It will not be located properly in the window system.");
            return getDefault();
        }
        if (win instanceof ChartWindowTopComponent) {
            return (ChartWindowTopComponent) win;
        }
        Logger.getLogger(ChartWindowTopComponent.class.getName()).warning(
                "There seem to be multiple components with the '" + PREFERRED_ID
                + "' ID. That is a potential source of errors and unexpected behavior.");
        return getDefault();
    }

   
    @Override
    public int getPersistenceType() {
        return TopComponent.PERSISTENCE_ALWAYS;
    }


    @Override
    public void componentOpened() {

        jbRefreshDataSource.doClick();

    }

    @Override
    public void componentClosed() {
        // TODO add custom code on component closing
    }

    void writeProperties(java.util.Properties p) {
        // better to version settings since initial version as advocated at
        // http://wiki.apidesign.org/wiki/PropertyFiles
        p.setProperty("version", "1.0");
        // TODO store your settings
    }

    Object readProperties(java.util.Properties p) {
        if (instance == null) {
            instance = this;
        }
        instance.readPropertiesImpl(p);
        return instance;
    }

    private void readPropertiesImpl(java.util.Properties p) {
        String version = p.getProperty("version");
        // TODO read your settings according to their version
    }

    @Override
    protected String preferredID() {
        return PREFERRED_ID;
    }

    private void initTabComponent(int i) {
        jtpPreview.setTabComponentAt(i, new ButtonTabComponent(jtpPreview));
    }
}
